package com.kangye.game.controller;

import com.alibaba.fastjson.JSON;
import com.kangye.game.pojo.TUser;
import com.kangye.game.service.UpdateService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: game_orders
 * @description: 修改用户信息控制器
 * @author: lys
 * @create: 2021-09-01 20:39
 **/
@RestController
@CrossOrigin
public class UpdateController {
    @Resource
    private UpdateService updateService;

    @RequestMapping("/updateTUser.action")
    public Object updateTUser(String loginname, String password, String username,
                              String tel, int id, int sex, @RequestParam MultipartFile file) throws IOException {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/");
//        String strFormat = simpleDateFormat.format(new Date());
        //获取上传图片文件名称
        String filename = file.getOriginalFilename();
//        //获取上传位置的相对路径
        File f = new File("a01_io12.JPG");
        //上传文件的相对路径
        String xpath = f.getPath();
        //设置文件上传到哪
        String path = xpath + filename;
        //创建路径--并且给路径赋值
        File files = new File(path);
        //通过commonsMultipartFile中的transferTo();方法，将上传文件写入自己创建的位置
        file.transferTo(files);

        TUser tUser = new TUser();
        tUser.setId(id);
        tUser.setLoginname(loginname);
        tUser.setPassword(password);
        tUser.setUsername(username);
        tUser.setTel(tel);
        tUser.setSex(sex);
        //将文件路径设置给TUser
        tUser.setHead(path);
        int i = updateService.updateTUser(tUser);
        Map<String, String> map = new HashMap<>();
        if (i != 0) {
            String code = JSON.toJSONString("200");
            map.put("code", code);
        } else {
            String code = JSON.toJSONString("400");
            map.put("code", code);
        }
        return map;
    }

    @RequestMapping("/updateMoney.action")
    public Object updateMoney(Integer id, Integer newbalance, Integer oldbalance) {
        System.out.println(newbalance + "________" + oldbalance);
        int i = updateService.updateMoney(id, newbalance, oldbalance);
        //查询充值后的用户余额
        long bla = updateService.findBalance(id);
        String blajs = JSON.toJSONString(bla);
        Map<String, String> map = new HashMap<>();
        if (i != 0) {
            String code = JSON.toJSONString("200");
            map.put("balance", blajs);
            map.put("code", code);
        } else {
            String code = JSON.toJSONString("400");
            map.put("code", code);
        }
        return map;
    }

}
